window.Muni = window.Muni || {};
Muni.InfoWindow = Muni.InfoWindow || {};

var defaultInfoWindowOptions = {
    showed: true,
    content: "",
    maxWidth: 100,
    offsetPixels: 0
};

var prepareInfoWindow = function(cfg){
    var options = {};
    $.extend(options, defaultInfoWindowOptions,  cfg);
    return options;
};

Muni.InfoWindow.google = (function(){
    var infoWindows = [];

    return {
        addInfoWindow: function(map, cfg){
            var options = prepareInfoWindow(cfg);
            var position = new google.maps.LatLng(options.latitude, options.longitude);
            var gOptions = {
                position: position,
                content: options.content,
                maxWidth: options.maxWidth,
                offsetPixels: options.offsetPixels
            };
            var infoWindow = new google.maps.InfoWindow(gOptions);
            infoWindow.open(map);
            if(!options.show){
                infoWindow.close();
            };
            infoWindows.push(infoWindow);
            return infoWindow;
        },

        getInfoWindows: function(){
            return infoWindows;
        },

        removeInfoWindows: function(info){
            info.close();
            var idx = infoWindows.indexOf(info);
            if(idx != -1){
                infoWindows.splice(idx, 1);
            }
        }
    };
})();

Muni.InfoWindow.seznam = (function(){
    var infoWindows = [];

    return {
        addInfoWindow: function(map, cfg){
            var options = prepareInfoWindow(cfg);  
            var coords = SMap.Coords.fromWGS84(options.latitude, options.longitude);
            var infoWindow = new SMap.Card(options.maxWidth);
            infoWindow.getBody().innerHTML = options.content;
            map.addCard(infoWindow, coords, true);
            infoWindows.push(infoWindow);
            return infoWindow;            
        },

        getInfoWindows: function(){
            return infoWindows;
        },

        removeInfoWindow: function(map, info){
            map.removeCard();
            var idx = infoWindows.indexOf(info);
            if(idx != -1){
                infoWindows.splice(idx, 1);
            }
        }
    };
})();

Muni.InfoWindow.bing = (function(){
    var infoWindows = [];
    var map = null;

    return {
        addInfoWindow: function(bmap, cfg){
            map = bmap
            var options = prepareInfoWindow(cfg);

            var location = new Microsoft.Maps.Location(options.latitude, options.longitude);
            var bOptions = {
                //html doesn;t work
                description: options.content,
                visible: options.showed,
                width: options.maxWidth,
                offset: new Microsoft.Maps.Point(options.offsetPixels, options.offsetPixels)
            };
            var infoWindow = new Microsoft.Maps.Infobox(location, bOptions);

            map.entities.push(infoWindow);
            infoWindows.push(infoWindow);
            return infoWindow;
        },

        getInfoWindows: function(){
            return infoWindows;
        },

        removeInfoWindow: function(info){
            map.entities.remove(info);
            var idx = infoWindows.indexOf(info);
            if(idx != -1){
                infoWindows.splice(idx, 1);
            }
        }
        
    };
})();
